from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
from flask import json

client = AcsClient('LTAI4FvmLz4M5rKsoDYt7aFy', 'RUPkwztPp7bFdfDkJ1C0ghCiRY2IJN', 'cn-hangzhou')


class CCP(object):
    result = 0

    def send_template_sms(self, to, datas):
        """发送短信的辅助类"""
        request = CommonRequest()
        request.set_accept_format('json')
        request.set_domain('dysmsapi.aliyuncs.com')
        request.set_method('POST')
        request.set_protocol_type('https') # https | http
        request.set_version('2017-05-25')
        request.set_action_name('SendSms')

        request.add_query_param('RegionId', "cn-hangzhou")
        request.add_query_param('PhoneNumbers', to)
        request.add_query_param('SignName', "新闻资讯个人使用")
        request.add_query_param('TemplateCode', "SMS_175542880")
        request.add_query_param('TemplateParam', {'code': datas})

        response = client.do_action(request)
        CCP.result = json.loads(str(response, encoding='utf-8'))
        print(CCP.result)


if __name__ == '__main__':
    cpp = CCP()
    cpp.send_template_sms(to='15972929313', datas='5921')
